﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using DICE_GS;

public class DailyMissionListCell : MonoBehaviour
{
    // 업적셀 프리펩
    static GameObject s_ListCellPrefab;
    // 업적셀 프리펩 리소스 위치
    const string CellPrefabPath = "Prefabs/DailyMission/DailyMissionListCell";
    // 업적 정보
    static IDictionary<int, CAchievementData> s_AchievementDataDic;
    static int[] s_SortedKeys;

    // 업적 이름 및 달성카운트 및 보상여부
    public UILabel m_DailyMissionNameLabel;
    public UILabel m_DailyMissionObjectBodyLabel;
    public UILabel m_DailyMissionReward1Label;
    public UILabel m_DailyMissionReward2Label;

    CAchievementData m_AchievementData;
    AchievementInfo m_AchievementInfo;
    int m_CellIndex = -1;

    bool m_IsComplete = false;
    bool m_IsCanReward = false;

    static public DailyMissionListCell SetCell(int index, Transform parent, UIScrollView scrollView)
    {
        if (s_ListCellPrefab == null)
            s_ListCellPrefab = Resources.Load(CellPrefabPath) as GameObject;

        if (s_AchievementDataDic == null)
        {
            s_AchievementDataDic = CAchievementDataManager.Instance.GetAchievementMap();

            ICollection<int> temp = s_AchievementDataDic.Keys;

            s_SortedKeys = new int[temp.Count];
            temp.CopyTo(s_SortedKeys, 0);
            Array.Sort(s_SortedKeys);
        }

        GameObject tempOBJ = Instantiate(s_ListCellPrefab) as GameObject;

        tempOBJ.transform.parent = parent;       
        tempOBJ.transform.localScale = Vector3.one;      

        UIDragScrollView tempDS = tempOBJ.GetComponent("UIDragScrollView") as UIDragScrollView;
        tempDS.scrollView = scrollView;


        DailyMissionListCell tempAC = tempOBJ.GetComponent("DailyMissionListCell") as DailyMissionListCell;

        return tempAC;
    }

    public void SetCellWithMissionID(int missionID, int index)
    {        
        m_AchievementData = CAchievementDataManager.Instance.GetAchievementData(missionID);
       
        m_DailyMissionNameLabel.text = m_AchievementData.title_body;
        m_DailyMissionObjectBodyLabel.text = m_AchievementData.object_body;
        //m_DailyMissionRewardLabel.text =

        int charIndex = m_AchievementData.reward_01.IndexOf(",");
        //gold, 2000
        string reward0 = m_AchievementData.reward_01.Remove(charIndex);
        string reward1 = m_AchievementData.reward_01.Remove(0, charIndex + 2);


        int achievementCount = 0;
        LogCounterType daily = LogCounterType.DAILY_MISSION_ORIGIN + index + 1;

        foreach (AchievementValue tempValue in MyInfo.Instance.AchievementValueList)
        {
            if (tempValue.achievementLogname == (int)daily)
            {
                achievementCount = tempValue.achievementData;
                break;
            }
        }

        string reward2 = string.Format("{0} / {1}", achievementCount, m_AchievementData.log_count);

        m_DailyMissionReward1Label.text = string.Format("보상 : {0} {1}", reward1, reward0);

        foreach (AchievementInfo tempInfo in MyInfo.Instance.AchievementList)
        {
            if (tempInfo.achievementId == missionID)
            {
                if (tempInfo.achievementLevel == 0)     // 어치브 달성 못함
                    break;

                reward2 = "완료";
                break;
            }
        }

        m_DailyMissionReward2Label.text = reward2;
    }

    //void OnRewardButton(GameObject button)
    //{
    //    LoadingWindow.window.ShowShortLoading();        
    //    RestAPI.singleton.GetMyAchievementReward(CallbackOfGetMyAchievementReward, m_AchievementData.id);
    //}

    //void CallbackOfGetMyAchievementReward(Hashtable returnData)
    //{
    //    LoadingWindow.window.HideLoading();
    //    m_AchievementInfo.isReward = true;
    //    SetCellWithIndex(m_CellIndex);

    //    int rewardType = 0;  // 1 골드 2 다이아몬드

    //    if (m_AchievementData.reward_01.Contains("gold"))       
    //        rewardType = 1;
    //    else if (m_AchievementData.reward_01.Contains("diamond"))       
    //        rewardType = 2;
            

    //    int delIndex = m_AchievementData.reward_01.IndexOf(" ");
    //    Debug.LogError("delIndex ------------ " + delIndex);
    //    string valueString = m_AchievementData.reward_01.Remove(0, delIndex + 1);

    //    int value = int.Parse(valueString);
    //    Debug.LogError("value ------------ " + value);
    //    switch (rewardType)
    //    {
    //        case 1:
    //            MyInfo.Instance.Money += value;
    //            break;
    //        case 2:
    //            MyInfo.Instance.Diamond += value;
    //            break;
    //        //case 3:
    //        //    break;
    //    }
    //    //MyInfo.Instance.Money += m_AchievementData.reward_01
    //    TopInfoBar.instance.UpdateInfos();
    //}
}
